import maya.cmds as cmds;
import createPin;

cmds.SelectAll();
cmds.Delete();

outsideDiameter = 85.24;
insideDiameter = 74.28;
heightToTopPeg = 7.0;
pegDiameter = 4.2;
peg2peg = 63.9;
pegOffset = 5.6;#peg isn't in center of circle

#height = 2*(heightToTopPeg - pegDiameter) + pegDiameter;
height = 9.3;#changed this so I can change pegDiameter independently
wireChannelThickness = 2.0;
wireChannelDepth = height / 2.0;

bandWidth = 25;
bandThickness = 4.6;# fudge to make boolean easier
bandRadius = 83.63;#measured from headset band, using chord to radius formula
bandDiameter = bandRadius*2.0;
bandBlockOffset = 5.0;
bandInsideHole = 2.0;
bandWallThickness = 1.0;

pinHeight =  10.0;
pinRadius = 4.0;

#using cylinders vs pipes because of pipe bugs
outside = cmds.polyCylinder(r=outsideDiameter/2.0,h=height,sx=40);
inside = cmds.polyCylinder(r=insideDiameter/2.0,h=height*2,sx=40);

peg = cmds.polyCylinder(r=pegDiameter/2.0,h=(outsideDiameter+insideDiameter)/2.0,sx=40);
cmds.move(pegOffset,0,0);
cmds.rotate('90deg',0,0);



cmds.select(outside[0],inside[0]);
hull = cmds.polyBoolOp(op=2);
# hullCutter = cmds.duplicate(hull[0]);
# cmds.move(0,height,0);
cmds.select(hull[0]);


borderBlockWidth = outsideDiameter/2;
borderBlock = cmds.polyCube(w=borderBlockWidth, h=height*2,d=outsideDiameter*1.5);
cmds.select(borderBlock[0],hull[0]);
cmds.align(x='min',alignToLead=True);


cmds.select(hull[0],borderBlock[0]);
hull = cmds.polyBoolOp(op=3);#order matters

fudge = 0.1;
flatSection = cmds.polyCube(w=(pegOffset+fudge), h=height,d=outsideDiameter);
cmds.move(pegOffset/2.0-fudge/2.0,0,0);
cmds.select(hull[0],flatSection[0]);
hull = cmds.polyBoolOp(op=1);
 
rounder = cmds.polyCylinder(r=height/2.0,h=outsideDiameter,sx=40);
cmds.move(pegOffset,0,0);
cmds.rotate('90deg',0,0);
 
cmds.select(rounder[0],hull[0]);
hull = cmds.polyBoolOp(op=1,useThresholds=1);#useThresholds had to be set to 1 for this to work
#figured this out by trying the operation using maya and seeing what was in the log when it worked
 
 



removeBlock1 = cmds.polyCube(w=(height/2.0+pegOffset+fudge), h=height*2,d=insideDiameter);
cmds.select(removeBlock1[0],hull[0]);
cmds.align(x='max',alignToLead=True);
cmds.select(hull[0],removeBlock1[0]);
hull = cmds.polyBoolOp(op=2);

cmds.select(hull[0],peg[0]);
hull = cmds.polyBoolOp(op=1);

removeBlock2 = cmds.polyCube(w=(height/2.0+pegOffset+fudge), h=height*2,d=peg2peg);
cmds.select(removeBlock2[0],hull[0]);
cmds.align(x='max',alignToLead=True);
cmds.select(hull[0],removeBlock2[0]);
hull = cmds.polyBoolOp(op=2);


#create wirechannel
centerDiameter = (outsideDiameter+insideDiameter)/2.0;
wireOutsideDiameter = centerDiameter+wireChannelThickness;
wireInsideDiameter = centerDiameter-wireChannelThickness;
outside = cmds.polyCylinder(r=wireOutsideDiameter/2.0,h=wireChannelDepth,sx=40);
inside = cmds.polyCylinder(r=wireInsideDiameter/2.0,h=wireChannelDepth,sx=40);
cmds.select(outside[0],inside[0]);
wireGuide = cmds.polyBoolOp(op=2);



#cut in half
borderBlockWidth = wireOutsideDiameter/2;
borderBlock = cmds.polyCube(w=borderBlockWidth, h=height*2,d=outsideDiameter*1.5);
cmds.select(borderBlock[0],wireGuide[0]);
cmds.align(x='min',alignToLead=True);

cmds.select(wireGuide[0],borderBlock[0]);
wireGuide = cmds.polyBoolOp(op=3);#order matters
# 
fudge = 0.1;
flatSectionWidth =pegOffset+wireChannelThickness/2.0;#pegOffset + fudge puts the edge in line with center of peg, we need half of the 
#wireChannelThickness in order to create guide
flatSection1 = cmds.polyCube(w=flatSectionWidth, h=wireChannelDepth,d=wireChannelThickness);
cmds.move(flatSectionWidth/2.0,wireChannelDepth/2.0,centerDiameter/2.0);
flatSection2 = cmds.polyCube(w=flatSectionWidth, h=wireChannelDepth,d=wireChannelThickness);
cmds.move(flatSectionWidth/2.0,wireChannelDepth/2.0,-centerDiameter/2.0);

cmds.select(flatSection1[0],wireGuide[0]);
cmds.align(y='min',alignToLead=True);
wireGuide = cmds.polyBoolOp(op=1);

cmds.select(flatSection2[0],wireGuide[0]);
cmds.align(y='min',alignToLead=True);
wireGuide = cmds.polyBoolOp(op=1);

flatSection3 = cmds.polyCube(w=wireChannelThickness, h=wireChannelDepth,d=centerDiameter);
cmds.move(pegOffset,wireChannelDepth/2.0,0);

#cmds.select(flatSection3[0],wireGuide[0]);
#wireGuide = cmds.polyBoolOp(op=1);
# 
# 
#fudge = 0.1;
#removeBlock3 = cmds.polyCube(w=flatSectionWidth, h=height/2.0,d=wireInsideDiameter);
#cmds.move((flatSectionWidth)/2.0,0,0);
#cmds.select(flatSection[0],removeBlock3[0]);
#flatSection = cmds.polyBoolOp(op=2);

#cmds.select(wireGuide[0],flatSection[0]);

 
cmds.select(wireGuide[0],hull[0]);
cmds.align(y='max',alignToLead=True);

# #offset block
block = cmds.polyCube(w=bandWidth/2.0, h=height + bandBlockOffset+pinHeight/2.0,d=bandWidth);
cmds.select(block[0],hull[0]);
cmds.align(x='min',alignToLead=True);
cmds.align(y='min',alignToLead=True);
cmds.select(block[0]);
cmds.move(-bandWidth/4-1.0,-pinHeight/2.0,0,relative=True);#0.5 for overlap reasons



#create band
 
bandOutsideDiameter = bandDiameter+bandThickness;
bandInsideDiameter = bandDiameter-bandThickness;
 
outside = cmds.polyCylinder(r=bandOutsideDiameter/2.0,h=bandWidth,sx=100);
inside = cmds.polyCylinder(r=bandInsideDiameter/2.0,h=bandWidth,sx=100);
cmds.select(outside[0],inside[0]);
band = cmds.polyBoolOp(op=2);
 
bandOutsideDiameter = bandDiameter+bandInsideHole;
bandInsideDiameter = bandDiameter-bandInsideHole;
# outside = cmds.polyCylinder(r=bandOutsideDiameter/2.0,h=bandWidth-bandWallThickness*2.0,sx=100);
# inside = cmds.polyCylinder(r=bandInsideDiameter/2.0,h=bandWidth-bandWallThickness*2.0,sx=100);

outside = cmds.polyCylinder(r=bandOutsideDiameter/2.0,h=bandWidth*2.0,sx=100);
inside = cmds.polyCylinder(r=bandInsideDiameter/2.0,h=bandWidth*2.0,sx=100);

cmds.select(outside[0],inside[0]);
bandInside = cmds.polyBoolOp(op=2);

cmds.select(bandInside[0],band[0]);
cmds.align(y='min',alignToLead=True);
cmds.select(bandInside[0]);
cmds.move(0,bandWallThickness,0,relative=True);

band2 = cmds.duplicate(bandInside[0]);
# cmds.xform(ws=True, rotatePivot=(-bandDiameter/2.0,0,0));#move pivot to edge of band so rotation makes more sense
# cmds.rotate('90deg',0,'70deg');
 
cmds.select(band[0],bandInside[0]);
band = cmds.polyBoolOp(op=2);

  
def createCurve(curveShape,borderBlockIn):   
    borderBlock = cmds.polyCube(w=bandOutsideDiameter/2.0, h=bandWidth*2,d=borderBlockIn);
    cmds.select(borderBlock[0],curveShape[0]);
    cmds.align(x='min',alignToLead=True);
    cmds.select(borderBlock[0]);
    cmds.move(0,0,-borderBlockWidth/2.0,relative=True)
    cmds.select(borderBlock[0],curveShape[0]);
    curveShape = cmds.polyBoolOp(op=3,useThresholds=1);
    cmds.select(curveShape[0]);
    cmds.xform(ws=True, rotatePivot=(-bandDiameter/2.0,0,0));#move pivot to edge of band so rotation makes more sense
    cmds.rotate('90deg',0,'70deg');
    cmds.move(bandDiameter/2.0-outsideDiameter/2.0,height/2.0-bandThickness,0);
    cmds.move(0,bandBlockOffset,0,relative=True);
    return curveShape;

borderBlockWidth = 60.0;

band = createCurve(band,borderBlockWidth);
band2 = createCurve(band2,borderBlockWidth);
   
cmds.xform( cp=True );#center pivot
   
# band2 = cmds.duplicate(band[0]);
  
# cmds.select(band2[0]);
# cmds.scale(1,wireChannelThickness/bandWidth,1,relative=True);
# cmds.move(bandThickness/2.0,bandThickness/2.0,bandWidth*0.3,relative=True);


   

cmds.select(block[0],band2[0]);
block = cmds.polyBoolOp(op=2);
 
# cmds.select(band[0],hullCutter[0]);
# band = cmds.polyBoolOp(op=2);
 
# cmds.select(band[0],hull[0]);
# hull = cmds.polyBoolOp(op=1);
 
pin = createPin.pin_solid( h=pinHeight, r=pinRadius);
cmds.select(pin[0]);
cmds.rotate(-90,90,0);
cmds.select(pin[0],hull[0]);
cmds.align(x='min',alignToLead=True);
cmds.select(pin[0]);
cmds.move(-pinHeight+pinHeight/4.0-0.25,0,0,relative=True);
   
wireHoleWidth = (((outsideDiameter-insideDiameter)/2.0)-wireChannelThickness)/2.0*1.5;
wireHole = cmds.polyCube(w=wireHoleWidth, h=wireChannelDepth+pinRadius/2.5,d=wireChannelThickness);
cmds.select(wireHole[0],hull[0]);
cmds.align(x='min',alignToLead=True);
cmds.align(y='max',alignToLead=True);
cmds.select(wireHole[0]);
cmds.move(-wireHoleWidth/4.0,0,0,relative=True);
   
   
cmds.select(hull[0],pin[0]);
hull = cmds.polyBoolOp(op=1);
   
   
#create hole between wireguide and peg
   
   
cmds.select(wireGuide[0]);
cmds.move(0,0.1,0,relative=True);
   
cmds.select(hull[0],wireHole[0]);
hull = cmds.polyBoolOp(op=2);
   
cmds.select(hull[0],wireGuide[0]);
hull = cmds.polyBoolOp(op=2);
   
cmds.select(hull[0],flatSection3[0]);#had to do this separately as it was removing some extra walls
hull = cmds.polyBoolOp(op=2);
   
# borderBlockWidth = outsideDiameter-insideDiameter+pinHeight;
# borderBlock = cmds.polyCube(w=borderBlockWidth, h=height*2,d=pinRadius*4.0);
# cmds.select(borderBlock[0],hull[0]);
# cmds.align(x='min',alignToLead=True);
   
# cmds.select(hull[0],borderBlock[0]);
# hull = cmds.polyBoolOp(op=3);#order matters
# block = cmds.polyCube(w=bandWidth/2.0, h=height + bandBlockOffset,d=bandWidth);
# testBlock = cmds.polyCube(w=bandWidth/2.0, h=height + bandBlockOffset,d=bandWidth);
# wireHole = cmds.polyCube(w=bandWidth, h=pinRadius*1.5,d=wireChannelThickness);
# cmds.select(wireHole[0],testBlock[0]);
# cmds.align(y='max',alignToLead=True);
# cmds.select(testBlock[0],wireHole[0]);
# testBlock = cmds.polyBoolOp(op=2);
   
   
# cmds.move(0,pinRadius,0)
   
createPin.pinHole( h=pinHeight, r=pinRadius);
pinHole = cmds.ls(sl=True)#work around
cmds.rotate(-90,90,0)
cmds.select(pinHole[0],block[0]);
cmds.align(x='max',alignToLead=True);
cmds.select(pinHole[0]);
cmds.move(0.01,0,0,relative=True);
#    
#    
cmds.select(block[0],pinHole[0]);
block = cmds.polyBoolOp(op=2);
cmds.select(band[0],block[0]);
band = cmds.polyBoolOp(op=1);
cmds.rotate(-90,0,0)


wireHole = cmds.polyCube(w=bandWidth/1.0, h=(height + bandBlockOffset+pinHeight*1.5)/2.0,d=wireChannelThickness);
cmds.select(wireHole[0],band[0]);
cmds.align(x='max',alignToLead=True);
cmds.align(y='max',alignToLead=True);
cmds.select(wireHole[0]);
cmds.move(bandWidth/2.0,0,0,relative=True);
cmds.select(band[0],wireHole[0]);
band = cmds.polyBoolOp(op=2);
 
wireHole2 = cmds.polyCube(w=bandWidth/8.0, h=(height + bandBlockOffset+pinHeight*1.5)/2.0,d=(height + bandBlockOffset+pinHeight/2.0)/2.0);
cmds.select(wireHole2[0],band[0]);
cmds.align(x='max',alignToLead=True);
cmds.align(y='max',alignToLead=True);
cmds.select(wireHole2[0]);
cmds.move(-bandWidth/2.0+bandWidth/8.0,0,-0.5*(height + bandBlockOffset+pinHeight/2.0)/2.0 + bandWallThickness,relative=True)
cmds.select(band[0],wireHole2[0]);
band = cmds.polyBoolOp(op=2);
 
 
cmds.select(band[0]);
cmds.rotate(0,180,0);
cmds.move(-insideDiameter/1.3,0,0,relative=True);
 
cmds.select(band[0],hull[0]);
cmds.align(y='min',alignToLead=True);
cmds.select(hull[0]);
cmds.Delete();
# cmds.group(band[0],hull[0]);
cmds.select(band[0]);
cmds.rotate('90deg',0,0);
 
    
    
    


